﻿/// <reference path="../../../fly.common/fly/fly.js" />
!function () {
    var $ = fly;
    $.doc.addClass(fly.box.isFromShare ? 'from-share with-share' : (fly.box.isShareTo ? 'share-to with-share' : ''))
    var rootName = fly.box.isFromShare ? '@Ta 们分享的' : '@我分享的'
    var serviceClass = fly.box.isFromShare ? 'FromShare' : 'ShareTo'
    var fm, FM = fly.box.FileManage;
    FM.extend({
        serviceClass: serviceClass,
        paths: [{ id: "", fileId: '', title: rootName, name: '<b><i></i>{0}</b>'.format(rootName), index: 0}],
        handeFiles: function (files) {
            files.fMap = {}
            files.each(function (f) {
                files[f.id] = files.fMap[f.fileId] = f;
                f.type = fly.box.typeMap[f.type];
                f.extension = fly.getExtension(f.name);
            });
            return files;
        },
        init: FM.prototype.init.after(function () {
            fm = this
        })
    })

    if (fly.box.isShareTo) {
        FM.extend({
            init: FM.prototype.init.after(function () {
                $('#menu-delete').text('取消共享');
            }),
            remove: function () {
                var me = this;
                var ids = this.selectionModel.getSelections('选择要删除的分享').select('o=>o.id')
                if (!ids[0])
                    return;
                $.confirm('要取消这些共享吗？ ' + ids.length, function () {
                    $.post({
                        url: fly.box.ajaxUrl(me.serviceClass, 'Delete'),
                        data: { ids: ids.join(',') },
                        success: function (result) {
                            if (result.success) {
                                me.reloadFiles();
                            }
                            else
                                $.alert(result.msg)
                        },
                        onError: $.onAjaxErr
                    })
                })
            }
        })
    } else {
        FM.extend({
            createMembers: FM.prototype.createMembers.after(function () {
                this.nodeTemplate = this.listView.nodeTemplate = this.nodeTemplate.replace(/<div[^>]*class=node-commands[^>]*>/i, function ($0) {
                    return $0 + '<a class=node-cmd-saveTo href="javascript:;" title="转存至我的网盘" onclick="manager.saveTo(\'{node.fileId}\')"></a>'
                })
            }),
            doSaveTo: function (ids, folderId, cb) {
                $.post({
                    url: fly.box.ajaxUrl(fm.serviceClass, 'SaveTo'),
                    data: { ids: ids.join(','), folderId: folderId },
                    success: function (result) {
                        if (result.success) {
                            cb && cb()
                        }
                        else
                            $.alert(result.msg)
                    },
                    onerror: $.onAjaxErr
                })
            },
            saveTo: function (id) {
                if (!id) {
                    id = this.getSelectFiles().select('o=>o.fileId')
                    if (!id[0])
                        return;
                }

                fly.common.createDialog({
                    title: '请选择目标文件夹',
                    url: this.indexPath + "file/select.htm?parentId=0&parentName=@我的网盘",
                    width: 300,
                    height: 400,
                    resizeable: false,
                    hideOnClose: false,
                    bottomItems: [{
                        text: "确定",
                        handler: function () {
                            var item = fm.saveToDialog.iframeElement[0].getSelection()[0];
                            if (!item) {
                                $.alert('请选择转存的目标文件夹。');
                                return
                            }

                            fm.doSaveTo(id, item.id, function () {
                                $.alertAuto('文件已转存至“{0}”'.format(item.text))
                                fm.saveToDialog.close()
                            })
                        }
                    }]
                }, function (dialog) {
                    fm.saveToDialog = dialog
                    dialog.show(true)
                }, this.dWin)
            }
        })
    }
} ();